package com.managedBean.registration;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.brickred.socialauth.Profile;
import org.primefaces.context.RequestContext;

import com.controller.cart.LoginController;
import com.dto.merchant.UserDTO;
import com.utils.ApplicationUtils;
import com.utils.JsfUtils;

@ManagedBean(name = "login")
@SessionScoped
public class LoginManagedBean implements Serializable {

	/**
	 * 9640913018 Anu 9949010240 appu 9966181357 mani0810
	 */
	private static final long serialVersionUID = 6416987679539296724L;
	private String mobileNo;
	private String pwd;
	private boolean loginSuccess;
	@ManagedProperty(value="#{loginController}")
	private LoginController loginController;
	private UserDTO userDTO;
	
	@PostConstruct
	public void init(){
		loginSuccess=false;
	}
	public void login() {
		try {
			if (validateLogin()) {
				RequestContext.getCurrentInstance().execute("dlg.hide()");
				RequestContext.getCurrentInstance().execute("topRightMenu");
				loginSuccess=true;
				userDTO=retrieveUserDetails();
				RegistrationManagedBean bean = (RegistrationManagedBean) JsfUtils
						.getBeanInstanceFromFacesContext("registration");
				bean.setName(userDTO.getName());
				bean.setMobileNo(userDTO.getMobileno());
				bean.setEmail(userDTO.getEmail());
				bean.setHouseNo(userDTO.getHouseno());
				bean.setStreet(userDTO.getStreet());
				bean.setArea(userDTO.getArea());
				bean.setCity(userDTO.getCity());
				bean.setLandmark(userDTO.getLandmark());
				bean.setShowRegistrationDetailsFlag(true);
				bean.setIduserTable(userDTO.getIduserTable());
				bean.setChkOutType("E");
			}else{
				addMessage("Invalid Login");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void facebookLogin(Profile profile){
		RegistrationManagedBean bean = (RegistrationManagedBean) JsfUtils
		.getBeanInstanceFromFacesContext("registration");
		loginSuccess=true;
		bean.setName(profile.getFullName());
		bean.setEmail(profile.getEmail());
		bean.setChkOutType("E");
	}
	public boolean validateLogin()  {
		boolean flag=false;
		try {
			flag= loginController.validateLogin(mobileNo, ApplicationUtils.securePassword(pwd));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	public void addMessage(String summary) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
				summary, null);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}
	private UserDTO retrieveUserDetails() {
		//TODO retrieve user details by mobileno;
		UserDTO dto=null;
		try {
			dto= loginController.retrieveUserDetails(mobileNo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dto;
	}

	

	/**
	 * @return the mobileNo
	 */
	public String getMobileNo() {
		return mobileNo;
	}
	/**
	 * @param mobileNo the mobileNo to set
	 */
	public void setMobileNo(String mobileNo) {
		this.mobileNo = mobileNo;
	}
	/**
	 * @return the pwd
	 */
	public String getPwd() {
		return pwd;
	}

	/**
	 * @param pwd
	 *            the pwd to set
	 */
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	/**
	 * @return the loginSuccess
	 */
	public boolean isLoginSuccess() {
		return loginSuccess;
	}
	/**
	 * @param loginSuccess the loginSuccess to set
	 */
	public void setLoginSuccess(boolean loginSuccess) {
		this.loginSuccess = loginSuccess;
	}
	/**
	 * @return the loginController
	 */
	public LoginController getLoginController() {
		return loginController;
	}
	/**
	 * @param loginController the loginController to set
	 */
	public void setLoginController(LoginController loginController) {
		this.loginController = loginController;
	}
	/**
	 * @return the userDTO
	 */
	public UserDTO getUserDTO() {
		return userDTO;
	}
	/**
	 * @param userDTO the userDTO to set
	 */
	public void setUserDTO(UserDTO userDTO) {
		this.userDTO = userDTO;
	}
	
	
}
